X-Git-Url: https://git.hcoop.net/clinton/website/site/unknownlamer.org.git/blobdiff_plain/922c2594cc6fcf74c963e18e56d0084782031abc..a7e21d41246bb6c3f3ee5cf8a7e854046f6e28d2:/Metaobject%20Protocols.html diff --git a/Metaobject Protocols.html b/Metaobject Protocols.html index 05901c2..99f9ed2 100644 --- a/Metaobject Protocols.html +++ b/Metaobject Protocols.html @@ -7,6 +7,9 @@ + + @@ -27,10 +30,10 @@
-Classes for scratch data and types +Classes for Scratch Data and Types
-Generics with methods that implement protocols +Generics with Methods that Implement Protocols
@@ -99,10 +102,10 @@
-Top level must call lower level functions +Top Level Must Call Lower Level Methods
-Lower level methods are easier to customize +Lower Level Methods are Easier to Customize
@@ -115,78 +118,65 @@ Memoization
-Cleaner Code +Constant Shared Return Values
Procedural Only Where Neccesary
- - -
-Examples -
-
-
-
-Object Inspector -
-
-Observer Design Pattern -
-Real World +Real World
-UCW and Arnesi +UCW and Arnesi
-CLSQL +CLSQL
-Elephant +Elephant
-Sources & Further Reading +Sources & Further Reading
-Sources +Sources
-The Art of the Metaobject Protocol +The Art of the Metaobject Protocol
-CLOS MOP Specification +CLOS MOP Specification
-Metaobject Protocols: Why We Want Them and What Else They Can Do +Metaobject Protocols: Why We Want Them and What Else They Can Do
-Why Are Black Boxes so Hard to Reuse? +Why Are Black Boxes so Hard to Reuse?
-Further Reading +Further Reading
-A Metaobject Protocol for C++ +A Metaobject Protocol for C++
-Open Implementations and Metaobject Protocols +Open Implementations and Metaobject Protocols
@@ -226,9 +216,8 @@ significantly more powerful as it encourages complete encapsulation through its use of classes primarily for method specialization rather than for state storage.

-

-Classes for scratch data and types

+Classes for Scratch Data and Types

In CLOS classes store data in slots (which are the same as data members). Encapsulation is not provided; any bit of code can use @@ -236,52 +225,53 @@ members). Encapsulation is not provided; any bit of code can use first, but encapsulation is of questionable importance as the slots are meant only to be used by the protocol defined around the class.

-

Classes are defined with defclass

+

Classes are defined with defclass

-(defclass name (superclasses ...)
+(defclass name (superclasses ...)
   ((slot-name :accessor slot-accessor ...)
    ...)
   (class-options ...))
 
-(defclass example ()
+(defclass example ()
   ((foo :accessor foo-of :initform 5)))
 
-(defclass example-child (example)
+(defclass example-child (example)
   ((bar :accessor bar-of :initform (list 1 2 3))))
 
-

Slot defintions have several option; the above example shows only the +

Slot defintions have several options; the above example shows only the :accessor and :initform options which are the most commonly used. :accessor generates an accessor for the slot (e.g. if you have -an instance of example you can (setf (foo-of some-example-instance) 'some-value) to set and (foo-of some-example-instance) to access the +an instance of example you can (setf (foo-of some-example-instance) +'some-value) to set and (foo-of some-example-instance) to access the value). :initform provides a default initial value for the slot as a -symbolic expression to be evaluated when an instance is created.

+symbolic expression to be evaluated when an instance is created in the +lexical environment of the class definition.

-Generics with methods that implement protocols

+Generics with Methods that Implement Protocols

Generics are like normal functions in Lisp, but they only provide a lambda list (parameter list). Methods are added to the generic which -specialize on the types of their parameters, and provide the actual -implementation. This allows for rich layered protocols to be developed -which can enable selective modification of individual facets with -minimal code.

+specialize on the types of their parameters and provide an +implementation. This allows writing rich layered protocols which can +enable selective modification of individual facets with minimal code.

-(defgeneric generic (parameters ...)
+(defgeneric generic (parameters ...)
   (options) ...)
 
-(defmethod generic-name ((parameter type) parameter ...)
+(defmethod generic-name ((parameter type) parameter ...)
   "documentation string"
   body)
 
-(defgeneric foo (bar baz quux)
+(defgeneric foo (bar baz quux)
   (:documentation "Process the baz with the quux capacitor to make the
 foo widget fly into the sky at warp speed"))
 
-(defmethod foo ((bar example) baz (quux capacitor))
+(defmethod foo ((bar example) baz (quux capacitor))
   (launch bar (process-with quux baz)))
 
@@ -298,34 +288,34 @@ reasons outside of the scope of this presentation.

Limitations of Default Language Behavior

The behavior of a language is a compromise between many competing -issues that attempts to be as generally useful as possible, and most -applications will have no issue with the default behavior. There are, -however, certain applications that could be cleanly written with minor -modifications to the behavior of the language, but would be impossible -or quite difficult to write otherwise.

+issues that attempts to be as generally useful as possible so that +most applications will have no issue with the default behavior. There +are, however, certain applications that could be cleanly written with +minor modifications to the behavior of the language, but would be +impossible or quite difficult to write otherwise.

Slot Storage

Most languages choose to preallocate storage for all of the slots of -an instance. Imagine a contact database that stored information about -people as slots of the class. There may be dozens of slots, but often -many of them will be left blank. If slot storage is preallocated much -memory will be wasted and the system may not be able to fit into the -memory of the hardware it must run on (perhaps for financial reasons, -huge datasets, etc.).

+an instance. Now imagine a contact database that stores information +about people in slots of a class. There may be dozens of slots, but +often many of them will be left blank. If slot storage is preallocated +much memory will be wasted and the database may not be able to fit +into the memory of the hardware it must run on (perhaps for financial +reasons, huge datasets, etc.).

To save memory the author of the contact database must implement his own system to store properties and allocate them lazily. This represents a fair bit of effort, and would implement a system that -differed from the existing slot system of classes only in the method -of data storage.

+differed from the existing slot system of classes only regarding slot +storage.

-

It would be useful if there were a way to customize instance -allocation. The customizations would be minor and require overriding +

It would be useful if there were a way to customize slot allocation in +instances. The customizations would be minor and require overriding only the initial allocation behavior and the behavior of the first assignment to the slot. It is a a trivial problem in a language that -allows customization of these.

+allows customization of these behaviors.

@@ -334,7 +324,9 @@ Design Patterns

Design Patterns are generalized versions of common patterns found in programs. Many of them are merely methods to get around deficiencies in the language, and can be quite messy to implement in some -languages.

+languages. Ideally a pattern would be subsumed by the language, but +real world contraints require language standards to remain fairly +static.

@@ -342,7 +334,7 @@ languages.

Metasoftware

Some types of programs could be written easily if the language were -customizable, but are nearly impossible to write when it is not.

+customizable but are nearly impossible to write when it is not.

Runtime Generated Classes

@@ -351,26 +343,25 @@ Runtime Generated Classes own objects, attach behaviors to the objects, and perhaps mix different objects together to create new ones. When you abstract the problem this looks just like an object system! Wouldn't it be nice if -your program could create new objects and methods on the fly portably?

+your program could create new classes and methods on the fly portably?

Object Inspection

-

Imagine if you were developing a complicated program with many -different objects that interacted in fairly complex ways. A tool to -inspect the structure of objects while debugging would be quite -useful, but in a traditional language would be impossible to implement -portably. This could force you to purchase a certain compiler -implementation which provided one, and even then would more than -likely not be customizable.

+

Imagine you were developing a complicated program with many different +objects that interacted in fairly complex ways. A tool to inspect the +structure of objects while debugging would be quite useful, but in a +traditional language would be impossible to implement portably. This +could force you to purchase a certain compiler implementation which +provided an inspector, and even then would likely not be customizable.

This problem can be generalized to apply to most debugging tools; it would be useful to write such tools portably because users of the language and not the compiler need to debug software. Sharing infrastructure would result in better tools (more developers), and -save man-years of wasted effort that comes with having to rewrite -non-portable functionality from scratch multiple times.

+save the man-years of wasted effort that comes with having to rewrite +unportable tools from scratch multiple times.

@@ -380,17 +371,17 @@ Metaobject Protocols

Limited/Generalized Internals of the Implementation

-

A Metaobject protocol is a generalized and limited subset of the -underlying implementation of the language. It is generalized and -limited in scope to allow for multiple implementation strategies; -this, along with careful design, is essential because programming -language research is ever advancing and new techniques for creating -more reliable and faster implementations are still being discovered.

+

A Metaobject Protocol (MOP) is a generalized and limited subset of the +underlying language implementation. It is limited to allow multiple +implementation strategies; this, along with careful design, is +essential because programming language research is ever advancing and +new techniques for creating more reliable and faster implementations +are still being discovered.

This subset of the implementation is exported as a set of methods on -metaobjects. Thus the system is implemented in itself. The system can -then be customized using the extension and overriding features of the -system.

+metaobjects. Thus the language is implemented in itself. The system +can then be customized using the extension and overriding features of +the language itself.

@@ -399,168 +390,38 @@ Classes of MOPs

Reflective

-

A reflective MOP provides a functional/procedural interface to -information about the system. It exposes class relationships, the -methods are attached to a generic, etc. A reflective MOP often -provides some functionality for creating new classes at runtime.

+

A reflective MOP provides an interface to information about the +running system. It exposes class relationships, the methods attached +to a generic, etc. A reflective MOP often provides some functionality +for creating new classes at runtime. Smalltalk was one of the first +languages to expose a reflective MOP.

Example: Object Inspector
- -
Example: Runtime Generated Classes and Methods
- - - -

-Intercessory

- -

Intercessory MOPs allow the user to customize language behavior by -implementing methods which override certain aspects of the language -behavior. This class of MOPs are what make MOPs especially -powerful. No longer must a problem be restructured to fit the -implementation language; the underyling language can be reshaped to -fit the task at hand, and obfuscation of the intended structure of the -application can be avoided.

- -
Example: Lazily Allocated Slots
- - -
Example: Observer Design Pattern
- - - - -

-Violation of Encapsulation?

- -

A MOP may seem like a violation of encapsulation by revealing some -implementation details, but in reality a well designed protocol does -not reveal anything which was not already exposed. Implementation -decisions affect users, and some of these details do leak through to -higher levels (e.g. the memory layout of slots). Implicit in the -protocol specification are these implementation details, and the MOP -merely makes this limited subset available for customization.

- -

A MOP makes it possible to customize certain implementation decisions -that do not radically alter the behavior of the base language. The -conceptual vocabulary of the system retains its meaning, and so code -written in one dialect can interact with code written in another -without knowing that they speak different ones.

- - - -

-MOP Design Principles

- -

-Layered Protocol

- -

A layered protocol design is good for both meta and normal object -protocols, and enables a combinatorial explosion of customizations to -the protocol.

- -

-Top level must call lower level functions

- -

The top level methods of a layered metaobject protocol are required to -call certain methods to perform some tasks. This both makes it easier -to customize the top level methods (which perform very broad tasks) by -providing some pieces of them for the programmer, and allows more -customization to be done by opening up the replacement of lower level -functions as a way to alter a small detail of the high level behavior.

- - -

-Lower level methods are easier to customize

- -

The lower level methods of a MOP are limited in scope and can be -implemented easily. Often the changes to language behavior that are -wanted are very small, and having methods that perform simple tasks -which are often customized reduces the effort required to extend the -system.

- - - -

-Functional Where Possible

- -

Functional protocols are preferred for MOPs (and object protocol in -general). Functional protocols open up several optimizations for the -implementation without burdening the user of the protocol.

- -

-Memoization

- -

Memoization is the process of saving the results of a function call -for future use. This avoids expensive recomputation of values which -have not changed (recall that a true function will always return the -same result when given the same arguments).

- -

A functional MOP can be optimized easily by exploiting this property -to memoize the return values of calls to expensive operations. A MOP -must be be very fast to avoid making programs unusably slow, and -memoization is able to give an appreciable speedup in many cases -without an insignificant burden on memory usage.

- -
Constant Shared Return Values
- -

Disallowing the modification of values returned by protocol methods -allows the implementation to return large data structures by reference -to avoid expensive copying without having to do expensive data -integrity checks.

- - - -

-Cleaner Code

- - - -

-Procedural Only Where Neccesary

- -

Some operations like method invocation are inheretly stateful and so -must use a procedural protocol. There is no benefit to be gained from -using a functional protocol, and indeed an attempt would result in -obtuse code that severely restricted the implementor. Do note that -only a very small part of method invocation is stateful (the actual -call), and most of it can be implemented functionally (e.g. computing -the discriminating function).

- - - -

-Examples

- -

-Object Inspector

- -

A primitive portable object inspector is presented here.

-
-(defgeneric example-inspect (instance)
+(defgeneric example-inspect (instance)
   (:documentation "Simple object inspector using CLOS MOP"))
 
-(defmethod example-inspect ((instance t))
+(defmethod example-inspect ((instance t))
   (format t "Simple Object~% Value: ~S~%" instance))
 
-(defmethod example-inspect ((instance standard-object))
-  (let ((class (class-of instance)))
+(defmethod example-inspect ((instance standard-object))
+  (let ((class (class-of instance)))
     (format t "Class: ~S, Superclasses: ~S~%"
             (class-name class)
             (mapcar #'class-name
                     (class-precedence-list class)))
-    (let ((slot-names (mapcar #'slot-definition-name
+    (let ((slot-names (mapcar #'slot-definition-name
                               (class-slots class))))
       (format t "Slots: ~%~{ ~S~%~}" slot-names)
       (inspect-loop slot-names instance #'example-inspect))))
 
-(defun inspect-loop (slots instance inspector)
+(defun inspect-loop (slots instance inspector)
   (format t "Enter slot to inspect or :pop to go up one level: ")
   (finish-output)
-  (let* ((slot (read))
+  (let* ((slot (read))
          (found-slot (member slot slots)))
-    (cond (found-slot
+    (cond (found-slot
            (funcall inspector (slot-value instance slot))
            (funcall inspector instance))
           ((eq slot :pop) t)
@@ -572,10 +433,27 @@ Object Inspector
 
-

-Observer Design Pattern

+
Example: Runtime Generated Classes and Methods
+ + + +

+Intercessory

+ +

Intercessory MOPs allow the user to customize language behavior by +implementing methods which override certain aspects of the language +behavior. This class of MOPs are what make MOPs especially +powerful. No longer must a problem be restructured to fit the +implementation language; the underyling language can be reshaped to +fit the task at hand, and obfuscation of the intended structure of the +application can be avoided.

+ +
Example: Lazily Allocated Slots
+ + +
Example: Observer Design Pattern
-

A simple implementation of the observer pattern is under 100 lines, +

A simple implementation of the observer pattern is under 100 lines, and the user level code requires only a single line of code to make any existing class observable.

@@ -590,59 +468,59 @@ details to the program.

 ;;; This metaclass adds a slot to instances which use it, and so the
 ;;; system is defined in its own package to avoid name conflicts
-(defpackage :observer
+(defpackage :observer
   (:use :cl #+sbcl :sb-mop)
   (:export observable register-observer unregister-observer))
 
-(in-package :observer)
+(in-package :observer)
 
 ;;; Metaclass
-(defclass observable (standard-class)
+(defclass observable (standard-class)
   ()
   (:documentation "Metaclass for observable objects"))
 
-(defmethod compute-slots ((class observable))
+(defmethod compute-slots ((class observable))
   "Add a slot for storing observers to observable instances"
   (cons (make-instance 'standard-effective-slot-definition
                        :name 'observers
                        :initform '(make-hash-table)
-                       :initfunction #'(lambda () (make-hash-table)))
+                       :initfunction #'(lambda () (make-hash-table)))
         (call-next-method)))
 
-(defmethod validate-superclass ((class observable)
+(defmethod validate-superclass ((class observable)
                                 (super standard-class))
   t)
 
-(defun register-observer (instance slot-name key closure)
+(defun register-observer (instance slot-name key closure)
   (register-observer-with-class (class-of instance)
                                 instance
                                 slot-name
                                 key
                                 closure))
 
-(defun unregister-observer (instance slot-name key)
+(defun unregister-observer (instance slot-name key)
   (unregister-observer-with-class (class-of instance)
                                   instance
                                   slot-name
                                   key))
 
-(defun get-observers (instance slot-name)
+(defun get-observers (instance slot-name)
   (get-observers-with-class (class-of instance)
                             instance
                             slot-name))
 
-(defun add-observer-table (instance slot-name)
+(defun add-observer-table (instance slot-name)
   (setf (gethash slot-name (slot-value instance
                                        'observers))
         (make-hash-table)))
 
-(defgeneric register-observer-with-class (class instance slot-name key closure))
-(defgeneric unregister-observer-with-class (class
+(defgeneric register-observer-with-class (class instance slot-name key closure))
+(defgeneric unregister-observer-with-class (class
                                             instance
                                             slot-name
                                             key))
 
-(defmethod register-observer-with-class ((class observable)
+(defmethod register-observer-with-class ((class observable)
                                          instance
                                          slot-name
                                          key
@@ -654,30 +532,30 @@ details to the program.

(add-observer-table instance slot-name))) closure)) -(defmethod unregister-observer-with-class ((class observable) +(defmethod unregister-observer-with-class ((class observable) instance slot-name key) (remhash key (gethash slot-name (slot-value instance 'observers)))) -(defmethod get-observers-with-class ((class observable) +(defmethod get-observers-with-class ((class observable) instance slot-name) (gethash slot-name (slot-value instance 'observers))) -(defmethod (setf slot-value-using-class) :before (new-value +(defmethod (setf slot-value-using-class) :before (new-value (class observable) instance slot) - (let ((slot-name (slot-definition-name slot))) - (if (not (eq 'observers slot-name)) - (let ((observers + (let ((slot-name (slot-definition-name slot))) + (if (not (eq 'observers slot-name)) + (let ((observers (get-observers instance (slot-definition-name slot)))) - (if observers - (maphash #'(lambda (key observer) + (if observers + (maphash #'(lambda (key observer) (funcall observer - (if (slot-boundp instance slot-name) + (if (slot-boundp instance slot-name) (slot-value instance slot-name) nil) new-value)) @@ -685,10 +563,108 @@ details to the program.

-

+ + + +

+Violation of Encapsulation?

+ +

A MOP may seem like a violation of encapsulation by revealing some +implementation details, but in reality a well designed protocol does +not reveal anything which was not already exposed. Implementation +decisions affect users, and some of these details do leak through to +higher levels (e.g. the memory layout of slots). Implicit in the +protocol specification are these implementation details, and the MOP +merely makes this limited subset available for customization.

+ +

A MOP makes it possible to customize certain implementation decisions +that do not radically alter the behavior of the base language. The +conceptual vocabulary of the system retains its meaning, and so code +written in one dialect can interact with code written in another +without knowing that they speak different ones.

+ + + +

+MOP Design Principles

+ +

+Layered Protocol

+ +

A layered protocol design is good for both meta and normal object +protocols, and enables a combinatorial explosion of customizations to +the protocol.

+ +

+Top Level Must Call Lower Level Methods

+ +

The top level methods of a layered protocol are required to call +certain lower level methods to perform some tasks. This both makes it +easier to customize the top level methods (which perform very broad +tasks) by providing some pieces of implementation for the programmer, +and enables more customization by opening up the replacement of lower +level functions as a way to alter a small detail of the high level +behavior.

+ + +

+Lower Level Methods are Easier to Customize

+ +

The lower level methods of a MOP are limited in scope and can be +implemented easily. Often the desired changes to language behavior are +minor, and having methods that perform simple tasks which are often +customized reduces the effort required to extend the system.

+ + + +

+Functional Where Possible

+ +

Functional protocols are preferred for MOPs (and object protocols in +general). Functional protocols open up several optimizations for the +implementation without burdening the user of the protocol.

+ +

+Memoization

+ +

Memoization is the process of saving the results of a function call +for future use. This avoids expensive recomputation of values which +have not changed (recall that a true function will always return the +same result when given the same arguments).

+ +

A functional MOP can be optimized easily by exploiting this property +to memoize the return values of calls to expensive operations. A MOP +must be be very fast to avoid making programs unusably slow, and +memoization is able to give an appreciable speedup in many cases +without a significant burden on memory usage.

+ + +

+Constant Shared Return Values

+ +

Disallowing modification of values returned by protocol methods allows +the implementation to return large data structures by reference to +avoid expensive copying without having to do expensive data integrity +checks or copying.

+ + + +

+Procedural Only Where Neccesary

+ +

Some operations like method invocation are inheretly stateful and so +must use a procedural protocol. There is no benefit to be gained from +using a functional protocol, and indeed an attempt would result in +obtuse code that severely restricted the implementian. Do note that +only a very small part of method invocation is stateful (the actual +call), and most of it can be implemented functionally (e.g. computing +the discriminating function).

+ + +

Real World

-

+

UCW and Arnesi

Arnesi uses the CLOS MOP to implement methods which are transparantly @@ -700,7 +676,7 @@ current continuation, and resumes it upon submission. The user level code is completely unaware of this.

-

+

CLSQL

CLSQL uses the reflective part of the CLOS MOP to map Common Lisp data @@ -709,23 +685,23 @@ allocation to map slots onto database columns or sql expressions (for implementing relational slots).

-

+

Elephant

Elephant uses the CLOS MOP to transparantly store any class to disk -and handle paging between the disk store and memory efficiently and -with no user intervention.

+and handle paging between the disk store and memory efficiently +without user intervention.

-

+

Sources & Further Reading

-

+

Sources

-

+

The Art of the Metaobject Protocol

Kiczales, Gregor et al. MIT Press 1991
@@ -736,20 +712,20 @@ useful.

-

+

CLOS MOP Specification

Specification of the MOP for CLOS defined in The Art of the Metaobject Protocol.

-

+

Metaobject Protocols: Why We Want Them and What Else They Can Do

A short overview of MOP design principles followed by three example metaobject protocols for Scheme.

-

+

Why Are Black Boxes so Hard to Reuse?

Transcription of a talk on the benefits of open implementations of @@ -760,17 +736,17 @@ metaobject protocols as a solution to most of the problems.

-

+

Further Reading

-

+

A Metaobject Protocol for C++

Example of a purely compile time MOP. It implements the functionality of a code walker and something similar to the Lisp macro system.

-

+

Open Implementations and Metaobject Protocols

It is a bit long, but it seems to follow a similar structure to AMOP @@ -806,9 +782,10 @@ notes, and so the 331 pages might not actually take that long to read.

-

Last Modified: - March 13, 2008

+ September 26, 2008

\ No newline at end of file